package kyssion.leetcode.num151_200;

import kyssion.leetcode.util.ShowUtil;
import kyssion.leetcode.util.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class code199_二叉树的右视图 {

    public static void main(String[] args) {
        TreeNode treeNode = TreeNode.create(
                new  int[]{
                        1,2,3,-1,5,-1,4
                }
        );
        List<Integer> list = new code199_二叉树的右视图().rightSideView(treeNode);
        ShowUtil.showList(list,false);
    }

    List<Integer> res;
    public List<Integer> rightSideView(TreeNode root) {
        res = new ArrayList<>();
        ans(root,0);
        return res;
    }

    private void ans(TreeNode root, int i) {
        if(root==null){
            return;
        }
        if(i>=res.size()){
            res.add(root.val);
        }else{
            res.set(i,root.val);
        }
        ans(root.left,i+1);
        ans(root.right,i+1);
    }
}
